$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'hos/appointment/list',
        datatype: "json",
        colModel: [			
			{ label: '编号', name: 'id', index: 'id', width: 50, key: true },
			{ label: '患者姓名', name: 'user.name', index: 'user_id', width: 80 },
			{ label: '医生姓名', name: 'sysUser.username', index: 'doctor_id', width: 80 },
			{ label: '科室', name: 'sysDeptEntity.name', index: 'name', width: 80 },
			{ label: '预约时间', name: 'appointmentDate', index: 'appointment_date', width: 80 }, 			
			{ label: '预约状态', name: 'appointmentStatus', index: 'appointment_status', width: 80, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-danger">待接诊</span>' :
                        value===1? '<span class="label label-warning">已取消</span>' :
                            '<span class="label label-success">已接诊</span>';
                },
            },
            {   label: '操作',
                name : 'appointmentStatus',
                width : 50,
                formatter: function (value, grid, rows, state) {
                    var df=rows.appointmentStatus;
                    var ddd = "password('" + df + "')";
                    var ccc = "date('" + df + "')";
                    var fff = "term('" + df + "')";
                    return value===0?('<a class="btn btn-primary" onclick="receive('+rows.id+')"><i class="fa fa-pencil-square-o"></i>&nbsp;接诊</a>'):value===2?('<a class="btn btn-primary" onclick="receive('+rows.id+')"><i class="fa fa-pencil-square-o"></i>&nbsp;查看</a>'):'';
                        ; }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    new AjaxUpload('#upload', {
        action: baseURL + "sys/oss/upload",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                // alert(r.url);
                console.info(r.url);
                $("#imag1").attr('src',r.url);
            }else{
                alert(r.msg);
            }
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showList2:false,
		title: null,
		appointment: {},
        userData:{},
        setSysDeptEntityList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.appointment = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "就诊";
            vm.getInfo(id)
		},
        check:function (event) {
            vm.showList2 = false;
        },
        change:function (event) {
            vm.showList2 = true;
        },
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.appointment.id == null ? "hos/appointment/save" : "hos/appointment/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.appointment),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "hos/appointment/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "hos/appointment/info/"+id, function(r){
                vm.appointment = r.appointment;
                vm.userData =r.appointment.user;
                vm.setSysDeptEntityList=r.appointment.setSysDeptEntityList;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});
function receive(id) {
    $.get(baseURL + "hos/appointment/info/"+id, function(r){
        // $("#appointmentId").val(r.)
        vm.showList = false;
        vm.title="就诊";
        vm.appointment = r.appointment;
        vm.userData =r.appointment.user;
        vm.setSysDeptEntityList=r.appointment.setSysDeptEntityList;
    });
}